#include "mainwindow.h"
#include "ui_mainwindow.h"

void MainWindow::intUI()
{
    label = new QLabel;
    label -> setMidLineWidth(150);
    label -> setText("当前文件");   
    ui->statusBar->addWidget(label);   
    progressbar = new QProgressBar;
    progressbar -> setMinimum(5);
    progressbar -> setMaximum(50);
    progressbar -> setValue(ui -> textEdit -> font().pointSize());
    ui -> statusBar -> addWidget(progressbar);
    spinbox = new QSpinBox;
    ui -> mainToolBar -> addWidget(new QLabel("字体大小"));
    ui -> mainToolBar -> addWidget(spinbox);
    fontcombobox = new QFontComboBox;
    ui -> mainToolBar -> addWidget(new QLabel("字体"));
    ui -> mainToolBar -> addWidget(fontcombobox);

    time = new QTimer(this);    
    time->start(100); //设置间隔时间
    searchwindow = new SearchWindow;
}

void MainWindow::lianjie()
{
    connect(spinbox,SIGNAL(valueChanged(int)),
            this,SLOT(on_spinBoxFontSize_valueChanged(int)));

    connect(fontcombobox,SIGNAL(currentIndexChanged(const QString&)),
            this,SLOT(on_comboFont_currentIndexChanged(const QString&)));
/**/
    connect(searchwindow, &SearchWindow::signal, this, &MainWindow::compare_and_locate);    //用于寻找和定位搜索文本
/**/
    connect(time, &QTimer::timeout, this, [&](){    //用于实时显示统计字数
        count_num_of_text();
        ui->numShow->setText(QString(num));
    });

    connect(time, &QTimer::timeout, this, &MainWindow::on_actionbaocun_triggered);
}

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    intUI();
    lianjie();
    setCentralWidget(ui->textEdit);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_actioncuti_triggered(bool checked)
{
    QTextCharFormat fff;
    fff=ui->textEdit->currentCharFormat();
    if(checked)
    {
        fff.setFontWeight(QFont::Bold);
    }
    else
    {
        fff.setFontWeight(QFont::Normal);
    }
    ui ->textEdit->mergeCurrentCharFormat(fff);

}

void MainWindow::on_spinBoxFontSize_valueChanged(int aFontSize)
{
    QTextCharFormat fff;
    fff.setFontPointSize(aFontSize);
    ui ->textEdit ->mergeCurrentCharFormat(fff);
    progressbar->setValue(aFontSize);
}

void MainWindow::on_comboFont_currentIndexChanged(const QString &argl)
{
    QTextCharFormat fff;
    fff.setFontFamily(argl);
    ui->textEdit->mergeCurrentCharFormat(fff);
}


void MainWindow::on_actionxieti_triggered(bool checked)
{
    QTextCharFormat fff;
    fff=ui->textEdit->currentCharFormat();
    if(checked)
    {
        fff.setFontItalic(QFont::Bold);
    }
    else
    {
        fff.setFontItalic(QFont::StyleNormal);
    }
    ui ->textEdit->mergeCurrentCharFormat(fff);
}

void MainWindow::on_actionxiahuaxian_triggered(bool checked)
{
    QTextCharFormat fff;
    fff=ui->textEdit->currentCharFormat();
    if(checked)
    {
        fff.setFontUnderline(QFont::Bold);
    }
    else
    {
        fff.setFontUnderline(QFont::StyleNormal);
    }
    ui ->textEdit->mergeCurrentCharFormat(fff);
}

void MainWindow::on_actionxinjian_triggered(bool checked)   //新建一个文件
{
    MainWindow * newTable = new MainWindow;
    newTable->show();
}

void MainWindow::on_actiondakai_triggered()
{

    QString curpath=QDir::currentPath();//保存当前路径

    QString dlgTitle="打开一个文件";

    QString filter="程序文件(*.h *.cpp);;文本文件(*.txt);;所有文件(*.*)";

    QString afilname=QFileDialog::getOpenFileName(this,dlgTitle,curpath,filter);

     if(afilname.isEmpty())
      {
          return;
      }

      openStreamText(afilname);
}

bool MainWindow::openStreamText(const QString afilname)
{
    QFile aFile(afilname);

    if(!aFile.exists())//如果文件不存在
        return false;

    if(!aFile.open(QIODevice::ReadOnly |QIODevice::Text))//如果不是以只读和文本方式打开文件返回false
        return false;

    QTextStream aStream(&aFile);//文本流读取文件

    aStream.setAutoDetectUnicode(true);//自动检测unicode便于显示汉字

    ui->textEdit->setPlainText(aStream.readAll());

    aFile.close();

    return true;
}

void MainWindow::on_actionsousuo_triggered()     //打开搜索窗口
{
    searchwindow->show();
}

void MainWindow::on_actionbaocun_triggered(bool checked)    //保存
{
    QString e= ui->textEdit->toPlainText();
        QFile file;
        file.setFileName(QFileDialog::getSaveFileName(this,QString("保存路径"),QString("/"),QString("TEXT(*.txt)")));
        file.open(QIODevice::WriteOnly | QIODevice::Text);
        file.write(e.toUtf8());
        file.close();
}
/**/
void MainWindow::compare_and_locate()
{
    QString str = ui->textEdit->toPlainText();  //获取文本内容
    QString str_to_find = searchwindow->getText();  //获取待查文本内容
    int len = str.size();
    int len_to_find = str_to_find.size();
    for ( int i = 0; i < len; )
    {
        bool ok = true;
        for ( int j = i; j < i + len_to_find; ++ j )    //逐个比较
        {
            if ( str[i] != str_to_find[j - i] ) //遇到不相等时结束
            {
                ok = false;
                break;
            }
        }
        if ( ok )   //对找到的位置进行处理
        {
            //
            i += len_to_find;
        }
        else i++;
    }
}

void MainWindow::count_num_of_text()
{
    QString str = ui->textEdit->toPlainText();  //获取文本内容
    num = str.size();        //获取文本数目
}

